package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.ValidateCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

/**
 * 代码使我快乐 Administrator on 2019/12/8.
 */
@RestController
@RequestMapping("/ValidateCode")
public class ValidateCodeController {

    @Autowired
    private JedisPool jedisPool;
    @Reference
    private ValidateCodeService validateCodeService;

    @RequestMapping("/send4Order")
    public Result send4Order(String telephone){
//        Integer validateCode = ValidateCodeUtils.generateValidateCode(6);
//        try {
//            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, telephone, validateCode.toString());
//        } catch (ClientException e) {
//            e.printStackTrace();
//            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
//        }
//
//        Jedis jedis = jedisPool.getResource();
//        jedis.setex(telephone + RedisMessageConstant.SENDTYPE_ORDER, 300, validateCode.toString());
//        jedis.close();
//        return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
        String sendType = RedisMessageConstant.SENDTYPE_ORDER;

        Result result = validateCodeService.send4Order(telephone, sendType);
        return result;
    }

    @RequestMapping("/send4Login" )
    public Result send4Login(String telephone){
        String sendType = RedisMessageConstant.SENDTYPE_ORDER;
        Result result = validateCodeService.send4Login(telephone, sendType);
        return result;
    }

}
